<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/17
 * Time: 上午8:56
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

/**
 * 患者列表
 *
 */
class Doctor extends Basecontroller
{



    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('doctor');

    }
    /**
     * 医生列表
     */
    public function index()
    {
        //搜索
        $request = Request::instance();
        $pageQuery = [];
        $option['doctor.is_deleted'] = ['<', 3];

        if ($request->isGet()) {
            $data = $request->param();

            if (isset($data['name']) && trim($data['name'])) {
                $option['doctor.name|doctor.username'] = array('like', '%' . trim($data['name']) . '%');
                $pageQuery['query']['name'] = trim($data['name']);

            }

        }
        $this->assign('search', isset($pageQuery['query']) ? $pageQuery['query'] : '');
        //  print_r($option);
        $list = $this->model
            ->field('doctor.*,doctor_department.department as department_name')
            ->join('doctor_department','doctor_department.id=doctor.depart_id')
            ->where($option)
            ->order('create_time desc')
            ->paginate(false, false, $pageQuery);
        //print_r($list);
        // print_r(Db::name('patient_case')->getLastSql());
        $this->assign('list', $list);
        $this->assign('url',$this->request->action());
        $this->assign('breadcrumb', '医生列表');
        return $this->fetch();

    }



    /**
     * 添加患者
     */
    public function add_doctor()
    {


        //获取部门列表
        $sql = "select * from doctor_department where is_deleted=1";
        $departs = Db::query($sql);

        $this->assign('departs', $departs);
        $this->assign('breadcrumb', '添加医生');
        return $this->fetch();

    }


    /**
     * 编辑患者
     */
    public function edit_doctor()
    {

        $data = $this->request->param();
        $this->assign('id', $data['id']);
        $list = Db::query("select doctor.* from doctor
          left join doctor_department ON doctor_department.id=doctor.depart_id
          where doctor.id=?", [$data['id']]);
        $this->assign('list', $list[0]);

        $sql = "select * from doctor_department where is_deleted=1";
        $departs = Db::query($sql);

        $this->assign('departs', $departs);

        $this->assign('breadcrumb', '编辑医生');
        return $this->fetch('add_doctor');
    }




}